﻿package {t_namespace}.dal.auto;

import com.bsf.db.DbConn;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;
import com.bsf.util.ConvertUtil;
import {t_namespace}.model.auto.*;

/** 代码自动生成工具自动生成,不要在这里写自己的代码，否则会被自动覆盖哦 - 车毅*/
public class {t_object}_base_dal
{
    public boolean add(DbConn conn, {t_object}_model model)
    {
		Object[] Par = new Object[]{
		<!-- BEGIN FieldList1 -->
		//{f_note1}
		model.{f_name_model1},<!-- END FieldList1 -->
		};
        int rev = conn.executeSql("insert into {t_object}(<!-- BEGIN FieldList2 -->{f_name2},<!-- END FieldList2 -->)"+
										"values(<!-- BEGIN FieldList3 -->?,<!-- END FieldList3 -->)", Par);
        return rev == 1;

    }

    public boolean edit(DbConn conn, {t_object}_model model)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        <!-- BEGIN FieldList4 -->
		//{f_note4}
		Par.add(model.{f_name_model4});<!-- END FieldList4 -->
		Par.add(model.{t_idFiled});

        int rev = conn.executeSql("update {t_object} set <!-- BEGIN FieldList5 -->{f_name5}=?,<!-- END FieldList5 --> where {t_mainkeyname}=?",  Par.toArray());
        return rev == 1;

    }

    public boolean delete(DbConn conn, {t_mainkeytype} {t_mainkeyname})
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add({t_mainkeyname});

        String Sql = "delete from {t_object} where {t_mainkeyname}=?";
        int rev = conn.executeSql(Sql, Par.toArray());
        if (rev == 1)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    public {t_object}_model get(DbConn conn, {t_mainkeytype} {t_mainkeyname})
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add({t_mainkeyname});
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.* from {t_object} s where s.{t_mainkeyname}=?");
        List<Map<String,Object>> ds = conn.executeList(stringSql.toString(), Par.toArray());
        if (ds != null && ds.size() > 0)
        {
			return createModel(ds.get(0));
        }
        return null;
    }

	public ArrayList<{t_object}_model> list(DbConn conn)
    {
		ArrayList<{t_object}_model> rs = new ArrayList<{t_object}_model>();
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.* from {t_object} s ");
        List<Map<String,Object>> ds = conn.executeList(stringSql.toString(), new Object[]{});
        if (ds != null && ds.size() > 0)
        {
			for (Map<String,Object> dr : ds)
			{
				rs.add(createModel(dr));
			}
        }
        return rs;
    }

	public {t_object}_model createModel(Map<String,Object> dr)
    {
        {t_object}_model o = new {t_object}_model();
		<!-- BEGIN FieldList6 -->
		//{f_note6}
		if(dr.containsKey("{f_name6}"))
		{
			o.{f_name_model6} = ConvertUtil.objTo{f_type6}(dr.get("{f_name6}"));
		}<!-- END FieldList6 -->
		return o;
    }
}
